package com.fare.taluo.application.impl.analyse;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.application.analyse.AirCompanyChannelContrastApplication;
import com.fare.taluo.application.fetl.SinglePlaneInfoApplication;
import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.core.domain.config.channel.SourceEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.em.TimePeriod;
import com.fare.taluo.fetl.domain.entity.SinglePlaneInfoEntity;
import com.fare.taluo.fusionCharts.FusionChartsTemplateModel;
import com.fare.taluo.fusionCharts.SimpleFusionChartsBuilder;
import com.fare.taluo.util.FusionChartsUtil_2;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.StringAndDateAndFileUtil;
import com.fare.taluo.util.flightInfo.FlightInfoSortUtil;
import com.fare.taluo.util.flightInfo.SameChannelContrastUtil;
import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.vo.report.SameChannelContrastVO;
import com.fare.taluo.vo.report.SpreadVo;
import com.fare.taluo.vo.report.XmlVO;

@SuppressWarnings("serial")
@Controller("airCompanyChannelContrastApplicationImpl")
@Scope("prototype")
public class AirCompanyChannelContrastApplicationImpl implements
		AirCompanyChannelContrastApplication {
	@Resource(name = "flightInfoApplication")
	private FlightInfoApplication flightInfoApplication;
	@Resource(name = "singlePlaneInfoApplication")
	private SinglePlaneInfoApplication singlePlaneInfoApplication;
	@SuppressWarnings("static-access")
	@Override
	public String getFusionChartsXMLStr(String fromCity, String toCity,
			String fromCityCode, String toCityCode, String flightNo,
			String flightStartDate, String flightEndDate, Integer futureDay,
			String[] periods, Integer selfStartPeriods, Integer selfEndPeriods,
			String airCompanyHbaseKey, String comparedChannelHbaseKey,
			String priceType,Locale locale) throws Exception {
		boolean isHasFlightNo = true;
		if (flightNo == null || "".equals(flightNo.trim())) {
			isHasFlightNo = false;
		}
		SourceEntity se = new SourceEntity();
		List<SourceEntity> comparedSource = new ArrayList<SourceEntity>();// 所有比较的渠道
		// 比较渠道为选中的渠道，如携程
			if(null != comparedChannelHbaseKey){
				for (String valueStr : comparedChannelHbaseKey.split(",")) {
					comparedSource.add(se.findByHbaseKey(valueStr.split("_")[0].trim()));
				}
			}
			
		
		SourceEntity airCompany = se.findByHbaseKey(
				airCompanyHbaseKey.split("_")[0]);// 官网
		List<String> flightDate = new ArrayList<String>();// 航班日期list
		flightDate = FusionChartsUtil_2.getDatesStr(flightStartDate,
				flightEndDate);
		List<String> dates = FusionChartsUtil_2.getDatesStrFormat(flightDate);// 去掉时间的“-”
		FusionChartsTemplateModel model = FusionChartsTemplateModel
				.getNewDefaultInstance();// 报表模型
		model.addChartAttr(FusionChartsTemplateModel.AN_Caption, fromCity
				+ " - " + toCity + (null==I18nUtil.getProperties(locale).get("官网渠道走势对比")? "官网渠道走势对比":I18nUtil.getProperties(locale).get("官网渠道走势对比")));
		model.addCategroyLabels(FusionChartsUtil_2
				.getDatesStrFormatMMdd(flightDate));
		// * 当有航班号的时候，查询 LP_FLIGHT rowkey：
		// 渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班时间(年月日时) ,用户所选择的时间段失效
		// * 当没有航班号的时候，LP_LINE_HOUR rowkey： 渠道_起点_终点_航班日期(年月日时)_（航空公司key）
		List<FlightInfoEntity> flights = null;
		FlightInfoEntity fe = new FlightInfoEntity();// 你妹的领域模型
		 Map<String,PlaneInfo> map =  new  HashMap<String,PlaneInfo>();
		Map<String,List<PlaneInfo>> channelMap =  new HashMap<String,List<PlaneInfo>>();
		if (isHasFlightNo) {
			flights = fe.findByFlightNo(flightNo);// 找出所有的航班号，因为可能有多个起飞时间
		} else {
			flights = fe.findByLine(fromCityCode, toCityCode,
					airCompany.getCarrierKey());
		}
		List<PlaneInfo> airCompanyData = new ArrayList<PlaneInfo>();
		
		if (isHasFlightNo) {
			// 拿到官网数据
				List<SinglePlaneInfoEntity> tempEntityList1 = singlePlaneInfoApplication.findPlaneInfo(airCompany.getHbaseKey(),airCompany.getCarrierKey(), fromCityCode, toCityCode, null, flightNo);
				//过滤航班日期
				tempEntityList1 = SameChannelContrastUtil.filterByDate(tempEntityList1,flightDate);
				airCompanyData = this.getSinglePlaneListByGuanWang(priceType, flightDate, tempEntityList1, airCompanyData);
				
			map = SameChannelContrastUtil.getPlaneToMap(flightDate, airCompanyData, map);
			// 那其他一个或者多个渠道数据
			for (SourceEntity channel : comparedSource) {
				List<PlaneInfo> channelData = new ArrayList<PlaneInfo>();
				if(null != channel){
						List<SinglePlaneInfoEntity> tempEntityList = singlePlaneInfoApplication.findPlaneInfo(channel.getHbaseKey(),airCompany.getCarrierKey(), fromCityCode, toCityCode, null, flightNo);
						//过滤航班日期
						tempEntityList = SameChannelContrastUtil.filterByDate(tempEntityList,flightDate);
						channelData = this.getSinglePlaneListByChannel(priceType, flightDate, tempEntityList, channelData);
						/*if(com.fare.taluo.util.StringUtils.checkList(tempEntityList)){
							channelData.addAll(SameChannelContrastUtil.getPlaneInfoList(tempEntityList));
						}*/
						
						
					if(channelData.size()>0){
						channelMap.put(channel.getHbaseKey()+"_"+channel.getCarrierKey()+"_"+channel.getShortName(),channelData);
						map = SameChannelContrastUtil.getPlaneToMap(flightDate, channelData, map);
					}
					channelData = null;
				}
				
			}
		} else {//官网渠道航线对比
			// 拿到官网数据
				List<SinglePlaneInfoEntity> tempEntityList = singlePlaneInfoApplication.findPlaneInfo(airCompany.getHbaseKey(),airCompany.getCarrierKey(), fromCityCode, toCityCode, null, null);
				//过滤航班日期
				tempEntityList = SameChannelContrastUtil.filterByDate(tempEntityList,flightDate);
				//过滤时间
				tempEntityList = SameChannelContrastUtil.timeFilter(periods,tempEntityList);
				
				airCompanyData = this.getSinglePlaneListByGuanWang(priceType, flightDate, tempEntityList, airCompanyData);

			map = SameChannelContrastUtil.getPlaneToMap(flightDate, airCompanyData, map);
			// 那其他一个或者多个渠道数据
			for (SourceEntity channel : comparedSource) {
				List<PlaneInfo> channelData = new ArrayList<PlaneInfo>();
				if(null != channel){
					
					List<SinglePlaneInfoEntity> tempChannelList = singlePlaneInfoApplication.findPlaneInfo(channel.getHbaseKey(),airCompany.getCarrierKey(), fromCityCode, toCityCode, null, null);				
					//过滤航班日期
					tempChannelList = SameChannelContrastUtil.filterByDate(tempChannelList,flightDate);
					//过滤时间段
					tempChannelList = SameChannelContrastUtil.timeFilter(periods,tempChannelList);
					channelData = this.getSinglePlaneListByChannel(priceType, flightDate, tempChannelList, channelData);
					if(channelData.size()>0){
						channelMap.put(channel.getHbaseKey()+"_"+channel.getCarrierKey()+"_"+channel.getShortName(),channelData);
						map = SameChannelContrastUtil.getPlaneToMap(flightDate, channelData, map);
					}
					channelData = null;
				}
				
			}
		}
		/*// 最终得到两个数据，官网数据，和渠道数据
		if ("lowest".equalsIgnoreCase(priceType)) {
			airCompanyData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatch(
							airCompanyData, flightDate);
			channelData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatch(
							channelData, flightDate);
		} else if ("highest".equalsIgnoreCase(priceType)) {
			airCompanyData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatchHighestPrice(
							airCompanyData, flightDate);
			channelData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatchHighestPrice(
							channelData, flightDate);
		}*/

		// 封装xml模型
		String airCompanySetsStr = FusionChartsUtil_2
				.generateDashedByFormat(FusionChartsUtil_2
						.getPriceValueByPlaneInfosAndFormat(airCompanyData,locale),locale);
		model.createDataSet(1L);
		model.addDataSetAttr(1L, FusionChartsTemplateModel.AN_Seriesname,(null==I18nUtil.getProperties(locale).get(airCompany.getShortName())? airCompany.getShortName():I18nUtil.getProperties(locale).get(airCompany.getShortName()))
				);
		model.addSetFullStrIntoDataSet(1L, airCompanySetsStr);
		model.addDataSetAttr(1L, FusionChartsTemplateModel.AN_RenderAs, "Line");// 以折现展示数据
		Long channelDataId = 2L;
		//渠道数据
		 for (Map.Entry<String, List<PlaneInfo>> entry : channelMap.entrySet()) {
			 try {
				 
				 String channelSetsStr = FusionChartsUtil_2
							.generateDashedByFormat(FusionChartsUtil_2
									.getPriceValueByPlaneInfosAndFormat(entry.getValue(),locale),locale);
					model.createDataSet(channelDataId);
					String dataSetName =  entry.getKey().split("_")[2].trim();;
					model.addDataSetAttr(channelDataId, FusionChartsTemplateModel.AN_Seriesname,
							(null==I18nUtil.getProperties(locale).get(dataSetName)? dataSetName:I18nUtil.getProperties(locale).get(dataSetName)));
					model.addSetFullStrIntoDataSet(channelDataId, channelSetsStr);
					model.addDataSetAttr(channelDataId, FusionChartsTemplateModel.AN_RenderAs, "Line");
				 
				 
					channelDataId++;
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		 }
		model.addStyleToObject("ToolTip",
				FusionChartsTemplateModel.STYLE_ToolTip_FontSize_15);// 全局style
		model.addStyleToObject("Caption",
				FusionChartsTemplateModel.STYLE_Caption_FontSize_15);// 全局style,标题15
		/*return SimpleFusionChartsBuilder.getInstance().buildXMLStr(model);*/
		 //获得价差表格list
			List<SameChannelContrastVO> spreadVos =  getSpreadListFormByLine(flightDate,map, getArray(airCompanyHbaseKey, comparedChannelHbaseKey));
			XmlVO xmlvo = new XmlVO();
			xmlvo.setXmlString(SimpleFusionChartsBuilder.getInstance().buildXMLStr(model));
			StringBuffer resultBuffer = new StringBuffer();
			String jsonStr = com.fare.taluo.util.JsonUtil.toJson(spreadVos);
			resultBuffer.append("{\"Total\":"+spreadVos.size()+","+ "\"Rows\":"+jsonStr+","+ "\"Xml\":"+com.fare.taluo.util.JsonUtil.toJson(xmlvo)+"}");	
			String json = resultBuffer.toString();
		    return json;
	}


private List<SameChannelContrastVO> getSpreadListFormByLine(List<String> flightDate,Map<String,PlaneInfo> map,String[] companyChannelHbaseKey){
		
		List<SameChannelContrastVO> vos = new ArrayList<SameChannelContrastVO>();
		for (String date : flightDate) {
			int i = 0;
			SameChannelContrastVO vo = new SameChannelContrastVO();
			for (String hbaseCarrierNameStr : companyChannelHbaseKey) {
				String hbaseCarrierName = hbaseCarrierNameStr.split("_")[0];
				String carrierName  = hbaseCarrierNameStr.substring(hbaseCarrierNameStr.lastIndexOf("_")+1);
				//设置本航
				if(i == 0){
					PlaneInfo p = map.get(hbaseCarrierName.trim()+"_"+date)==null?null:map.get(hbaseCarrierName.trim()+"_"+date);
					if(null != p){
						vo.setNativeCarrier(carrierName);
						vo.setDate(date);
						vo.setNativePrice(""+p.getLowerPrice());
					}else{
						vo.setDate(date);
						vo.setNativeCarrier(carrierName);
						vo.setNativePrice(null);
					}
					
				}else{
					//获得非本航的其它航
					PlaneInfo p = map.get(hbaseCarrierName.trim()+"_"+date)==null?null:map.get(hbaseCarrierName.trim()+"_"+date);
					SpreadVo svo = new SpreadVo();
					
					if(null != p){
						Double price = Double.parseDouble(p.getLowerPrice());
						svo.setCarrierName(carrierName);
						if(vo.getNativePrice()==null){
							svo.setSpread(null);
							svo.setSpreadAmount(null);
						}else{
							svo.setSpreadAmount(""+(price-Double.parseDouble(vo.getNativePrice())));
							svo.setSpread(SameChannelContrastUtil.getSpreadToString(""+price,vo.getNativePrice()));
							
							
						}
						svo.setPrice(""+price);
					}else{
						
						svo.setCarrierName(carrierName);//new SourceEntity().findByHbaseKey(hbaseCarrierName.split("_")[0].trim()).get(0).getShortName()
						svo.setSpread(null);
						svo.setSpreadAmount(null);
						svo.setPrice(null);
					}
					vo.getContrastList().add(svo);
				}
				i++;
			}
			vos.add(vo);
		}
		return vos;
	}
	
	
	private List<PlaneInfo> getPlaneInfoList(List<PlaneInfo> guanwangList,List<PlaneInfo> qudaoList){
		List<PlaneInfo> planeInfoList = new ArrayList<PlaneInfo>();
		planeInfoList.addAll(guanwangList);
		planeInfoList.addAll(qudaoList);
		return planeInfoList;
	}
	
	
	public String getFusionChartsXMLStrCabin(String fromCity, String toCity,
			String fromCityCode, String toCityCode, String flightStartDate,
			String flightEndDate, String airCompanyCabin,
			String comparedCabins, String priceType,Locale locale) throws Exception {
		SourceEntity se = new SourceEntity();
		SinglePlaneInfoEntity entity = new SinglePlaneInfoEntity();
		String[] compareCabinArr = comparedCabins.split(",");
		List<SourceEntity> comparedSource = new ArrayList<SourceEntity>();// 所有比较的渠道
		for (String hbaseKey : comparedCabins.split(",")) {
			if(StringAndDateAndFileUtil.checkString(hbaseKey)){
				comparedSource.add(se
						.findByHbaseKey(hbaseKey.trim().split("_")[4]));
			}
			
		}
		List<String> flightDate = new ArrayList<String>();// 航班日期list

		flightDate = FusionChartsUtil_2.getDatesStr(flightStartDate,
				flightEndDate);
		FusionChartsTemplateModel model = FusionChartsTemplateModel
				.getNewDefaultInstance();// 报表模型
		model.addChartAttr(FusionChartsTemplateModel.AN_Caption, fromCity
				+ " - " + toCity + (null==I18nUtil.getProperties(locale).get("官网渠道走势对比")? "官网渠道走势对比":I18nUtil.getProperties(locale).get("官网渠道走势对比")));
		model.addCategroyLabels(FusionChartsUtil_2
				.getDatesStrFormatMMdd(flightDate));
		 Map<String,PlaneInfo> map =  new  HashMap<String,PlaneInfo>();
		List<PlaneInfo> airCompanyData = new ArrayList<PlaneInfo>();
		Map<String,List<PlaneInfo>> channelMap =  new HashMap<String,List<PlaneInfo>>();
		
		// 拿到官网数据     ZH_ZH9802_头等舱_深航_20000
		String guanwangKey = airCompanyCabin.split("_")[4];
		String guanwangCarrierKey = airCompanyCabin.split("_")[0];
		String guanwangFlightNo = airCompanyCabin.split("_")[1];
		String guanwangCabinType = airCompanyCabin.split("_")[2];
	//	for (String date : flightDate) {// 航班日期
		
		/*	List<SinglePlaneInfoEntity> guanwangEntity = entity
					.findPlaneInfoByCondition(guanwangKey, guanwangCarrierKey,
							fromCityCode, toCityCode, null, guanwangFlightNo);*/
		List<SinglePlaneInfoEntity> guanwangEntity = entity
				.findPlaneInfoPropertyAll(guanwangKey, guanwangCarrierKey,
						fromCityCode, toCityCode, null, guanwangFlightNo);
			 if ("lowest".equalsIgnoreCase(priceType)) {
				 guanwangEntity = SameChannelContrastUtil.getLowestByLineEachDay(guanwangEntity, flightDate);
				} else if ("highest".equalsIgnoreCase(priceType)) {
					//否则找到最高价
					guanwangEntity = SameChannelContrastUtil.getHighestByLineEachDay(guanwangEntity, flightDate);
				}
			if (com.fare.taluo.util.StringUtils.checkList(guanwangEntity)) {
				for (SinglePlaneInfoEntity singlePlane : guanwangEntity) {
					PlaneInfo p = SameChannelContrastUtil.conver2Planeinfo(
							singlePlane, guanwangCabinType, priceType);
					airCompanyData.add(p);
				}
				
			} else {
				airCompanyData.add(null);
			}

	//	}
		/*List<SinglePlaneInfoEntity> listEntity = entity.findPlaneInfoByCondition(channelKey,carrierKey, fromCityCode, toCityCode, null, flightNo);
		 if ("lowest".equalsIgnoreCase(priceType)) {
			 listEntity = SameChannelContrastUtil.getLowestByLineEachDay(listEntity, flightDate);
			} else if ("highest".equalsIgnoreCase(priceType)) {
				//否则找到最高价
				listEntity = SameChannelContrastUtil.getHighestByLineEachDay(listEntity, flightDate);
			}
		 if(com.fare.taluo.util.StringUtils.checkList(listEntity)){
			 for (SinglePlaneInfoEntity singlePlane : listEntity) {
				 PlaneInfo p = SameChannelContrastUtil.conver2Planeinfo(singlePlane,cabinType,priceType);
				 infos.add(p);
			}
			
		 }else{
			 infos.add(null);
		 }*/
		
		map = SameChannelContrastUtil.getPlaneToMap(flightDate, airCompanyData, map);
		// 其他一个或者多个渠道数据
		for (String cabinInfo : compareCabinArr) {
			List<PlaneInfo> channelDataCabin = new ArrayList<PlaneInfo>();
			if(StringAndDateAndFileUtil.checkString(cabinInfo)){
				
				String channelKey = cabinInfo.split("_")[4].trim();
				String carrierKey = cabinInfo.split("_")[0].trim();
				String flightNo = cabinInfo.split("_")[1].trim();
				String cabinType = cabinInfo.split("_")[2].trim();
				String channelName = cabinInfo.split("_")[5].trim();
				//for (String date : flightDate) {// 航班日期
					List<SinglePlaneInfoEntity> listEntity = entity.findPlaneInfoPropertyAll(channelKey, carrierKey,
							fromCityCode, toCityCode, null, flightNo);
					 if ("lowest".equalsIgnoreCase(priceType)) {
						 listEntity = SameChannelContrastUtil.getLowestByLineEachDay(listEntity, flightDate);
						} else if ("highest".equalsIgnoreCase(priceType)) {
							//否则找到最高价
							listEntity = SameChannelContrastUtil.getHighestByLineEachDay(listEntity, flightDate);
						}
					
					if (com.fare.taluo.util.StringUtils.checkList(listEntity)) {
						for (SinglePlaneInfoEntity singlePlane : listEntity) {
							PlaneInfo p = SameChannelContrastUtil.conver2Planeinfo(
									singlePlane, cabinType, priceType);
							channelDataCabin.add(p);
						}
						
					} else {
						channelDataCabin.add(null);
					}
				//}
			}
			if(channelDataCabin.size()>0){
				channelMap.put(cabinInfo,channelDataCabin);
				map = SameChannelContrastUtil.getPlaneToMap(flightDate, channelDataCabin, map);
			}
		}

		// 官网数据
		String airCompanySetsStr = FusionChartsUtil_2
				.generateDashedByFormat(FusionChartsUtil_2.
						getPriceValueCabin(airCompanyData,locale),locale);
		model.createDataSet(1L);
		model.addDataSetAttr(1L, FusionChartsTemplateModel.AN_Seriesname,
				(null==I18nUtil.getProperties(locale).get(airCompanyCabin.split("_")[3])? airCompanyCabin.split("_")[3]:I18nUtil.getProperties(locale).get(airCompanyCabin.split("_")[3])));
		model.addSetFullStrIntoDataSet(1L, airCompanySetsStr);
		model.addDataSetAttr(1L, FusionChartsTemplateModel.AN_RenderAs, "Line");// 以折现展示数据
		Long channelDataId = 2L;
		//渠道数据
		 for (Map.Entry<String, List<PlaneInfo>> entry : channelMap.entrySet()) {
			   //System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
			 String channelSetsStr = FusionChartsUtil_2
						.generateDashedByFormat(FusionChartsUtil_2
								.getPriceValueCabin(entry.getValue(),locale),locale);
				model.createDataSet(channelDataId);
				String dataSetName =  entry.getKey().split("_")[5].trim();;
				model.addDataSetAttr(channelDataId, FusionChartsTemplateModel.AN_Seriesname,
						(null==I18nUtil.getProperties(locale).get(dataSetName)? dataSetName:I18nUtil.getProperties(locale).get(dataSetName)));
				model.addSetFullStrIntoDataSet(channelDataId, channelSetsStr);
				model.addDataSetAttr(channelDataId, FusionChartsTemplateModel.AN_RenderAs, "Line");
			 
			 
				channelDataId++;
		 }
		
		model.addStyleToObject("ToolTip",
				FusionChartsTemplateModel.STYLE_ToolTip_FontSize_15);// 全局style
		model.addStyleToObject("Caption",
				FusionChartsTemplateModel.STYLE_Caption_FontSize_15);// 全局style,标题15
		
		 //获得价差表格list
		List<SameChannelContrastVO> spreadVos =  getSpreadListForm(flightDate,map, getArray( airCompanyCabin, comparedCabins));
		XmlVO xmlvo = new XmlVO();
		xmlvo.setXmlString(SimpleFusionChartsBuilder.getInstance().buildXMLStr(model));
		StringBuffer resultBuffer = new StringBuffer();
		String jsonStr = com.fare.taluo.util.JsonUtil.toJson(spreadVos);
		resultBuffer.append("{\"Total\":"+spreadVos.size()+","+ "\"Rows\":"+jsonStr+","+ "\"Xml\":"+com.fare.taluo.util.JsonUtil.toJson(xmlvo)+"}");	
		String json = resultBuffer.toString();
	    return json;	
	}
private String[] getArray(String airCompanyCabin,String comparedCabins){
	List<String> tmpList = new ArrayList<String>();
	tmpList.add(airCompanyCabin);
	if(null != comparedCabins){
		String[] compareCabinArr = comparedCabins.split(",");
		for (String string : compareCabinArr) {
			if(StringAndDateAndFileUtil.checkString(string)){
				tmpList.add(string);
			}
		}
	}
	
	return (String[]) tmpList.toArray(new String[tmpList.size()]);
}
	
	
private List<SameChannelContrastVO> getSpreadListForm(List<String> flightDate,Map<String,PlaneInfo> map,String[] carrierFlightnoCabins){
		
		List<SameChannelContrastVO> vos = new ArrayList<SameChannelContrastVO>();
		for (String date : flightDate) {
			int i = 0;
			SameChannelContrastVO vo = new SameChannelContrastVO();
			for (String carrierFlightnoCabin : carrierFlightnoCabins) {
				
				//设置本航
				if(i == 0){
					PlaneInfo p = map.get(carrierFlightnoCabin.split("_")[4].trim()+"_"+date)==null?null:map.get(carrierFlightnoCabin.split("_")[4].trim()+"_"+date);
					vo.setNativeCarrier(carrierFlightnoCabin.split("_")[5].trim());
					vo.setDate(date);
					if(null != p && p.getCabins().size()>0){
						vo.setNativePrice(""+p.getCabins().get(0).getPrice());
					}else{
						vo.setNativePrice(null);
					}
					
				}else{
					//获得非本航的其它航
					PlaneInfo p = map.get(carrierFlightnoCabin.split("_")[4].trim()+"_"+date)==null?null:map.get(carrierFlightnoCabin.split("_")[4].trim()+"_"+date);
					SpreadVo svo = new SpreadVo();
					
					if(null != p){
						Double price = p.getCabins().size()>0?p.getCabins().get(0).getPrice():0.0;
						svo.setCarrierName(carrierFlightnoCabin.split("_")[5].trim());
						svo.setPrice(""+price);
						if(vo.getNativePrice()==null){
							svo.setSpread(null);
							svo.setSpreadAmount(null);
							svo.setPrice(null);
						}else{
							svo.setSpreadAmount(""+(price-Double.parseDouble(vo.getNativePrice())));
							svo.setSpread(SameChannelContrastUtil.getSpreadToString(""+price,vo.getNativePrice()));
							svo.setPrice(""+price);
							
						}
					}else{
						svo.setCarrierName(carrierFlightnoCabin.split("_")[5].trim());
						svo.setSpread(null);
						svo.setSpreadAmount(null);
						svo.setPrice(null);
					}
					vo.getContrastList().add(svo);
				}
				i++;
			}
			vos.add(vo);
		}
		return vos;
	}
	
	@Override
	public List<FlightInfoVO> getFlights(String hbaseKey, String fromCityName,
			String toCityName) {
		List<FlightInfoVO> vos = new ArrayList<FlightInfoVO>();
		SourceEntity se = new SourceEntity().findByHbaseKey(hbaseKey);
		if (se != null ) {
			vos = flightInfoApplication.findByConditions(se
					.getCarrierKey(), fromCityName, toCityName);
		}
		return vos;
	}
	private List<PlaneInfo> getSinglePlaneListByGuanWang(String priceType,List<String> flightDate,List<SinglePlaneInfoEntity> tempEntityList,List<PlaneInfo> airCompanyData ){
		if ("lowest".equalsIgnoreCase(priceType)) {
			tempEntityList = SameChannelContrastUtil.getLowestByLineEachDay(tempEntityList, flightDate);
		} else if ("highest".equalsIgnoreCase(priceType)) {
			//否则找到最高价
			tempEntityList = SameChannelContrastUtil.getHighestByLineEachDay(tempEntityList, flightDate);
		}
		if(com.fare.taluo.util.StringUtils.checkList(tempEntityList)){
			airCompanyData.addAll(SameChannelContrastUtil.getPlaneInfoList(tempEntityList));
		}
		if ("lowest".equalsIgnoreCase(priceType)) {
			airCompanyData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatch(
							airCompanyData, flightDate);
		} else if ("highest".equalsIgnoreCase(priceType)) {
			airCompanyData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatchHighestPrice(
							airCompanyData, flightDate);
		}
		return airCompanyData;
	}
	
	private List<PlaneInfo> getSinglePlaneListByChannel(String priceType,List<String> flightDate,List<SinglePlaneInfoEntity> tempChannelList,List<PlaneInfo> channelData ){
		if ("lowest".equalsIgnoreCase(priceType)) {
			tempChannelList = SameChannelContrastUtil.getLowestByLineEachDay(tempChannelList, flightDate);
		} else if ("highest".equalsIgnoreCase(priceType)) {
			//否则找到最高价
			tempChannelList = SameChannelContrastUtil.getHighestByLineEachDay(tempChannelList, flightDate);
		}
		if(com.fare.taluo.util.StringUtils.checkList(tempChannelList)){
			channelData.addAll(SameChannelContrastUtil.getPlaneInfoList(tempChannelList));
		}
		
		
		// 最终得到两个数据，官网数据，和渠道数据
		if ("lowest".equalsIgnoreCase(priceType)) {
			channelData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatch(
							channelData, flightDate);
		} else if ("highest".equalsIgnoreCase(priceType)) {
			channelData = FlightInfoSortUtil
					.generatePlaneInfosInTodayCrawlOrderByFlightBatchHighestPrice(
							channelData, flightDate);
		}
		return channelData;
	}
	/**
	 * 过滤时间段
	 * 
	 * @author Administrator
	 * 
	 */
	class FilterTimePeriod {
		List<TimePeriod> timePeriods = new ArrayList<TimePeriod>();

		public FilterTimePeriod(String[] periods) {
			if (periods == null || periods.length >= 4) {
				timePeriods.add(TimePeriod.ALL);
			} else {
				for (String s : periods) {
					timePeriods.add(TimePeriod.fromCode(s.trim()));// 添加各个指定的时间段
				}
			}
		}

		public boolean filter(PlaneInfo p) throws Exception {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			if (p == null)
				return false;
			for (TimePeriod t : timePeriods) {
				if (t.hasBetweenTime(sdf.parse(p.getFlightBatchStartTime()
						.trim()))) {
					return true;
				}
				// if(t.hasBetweenTime(p.getFlightStartTime())){
				// return true;
				// }
			}
			return false;
		}

		public List<TimePeriod> getTimePeriods() {
			return timePeriods;
		}
	}
}
